<?php

namespace App\Admin\Http\Controllers\Business;

use App\Admin\Http\Controllers\Controller;
use App\Admin\Eloquent\ProjectProfit;
use Illuminate\Http\Request;
use DB;

class RepaymentManagementController extends Controller
{
	public function index(Request $request){
//		$name = $request->only('name');
		$query= $request->only('name','start_at','end_at','project_name','status', 'collateral_licence_no');
		//查询所有融资人的还款记录
		$list_query = DB::table('projects as a')
			->join('project_profits as b', 'a.id' , '=', 'b.project_id')
			->join('codes as c', 'a.repayment', '=', 'c.code')
			->join('user_info as d', 'a.financier_id', '=', 'd.user_id')
			->where('b.type', '=', '1');

			if(!empty($query['project_name'])){
				$list_query = $list_query->where('a.projects_name' ,'like','%'.$query['project_name'].'%');
			}
			if(!empty($query['status']) || $query['status']=='0'){
				$list_query = $list_query->where('b.status' ,'=', $query['status']);
			}

			if(!empty($query['name']))
				$list_query = $list_query->where('d.name', 'like', '%'.$query['name'].'%');

			if(!empty($query['collateral_licence_no']))
				$list_query = $list_query->where('a.collateral_licence_no', 'like', '%'.$query['collateral_licence_no'].'%');

			//chunmeng.jiang@live.com start

			if(empty($query['start_at']))
				$query['start_at'] = date('2015-04-01'.' 00:00:00');

			if(empty($query['end_at']))
				$query['end_at'] = date('Y-m-d'.' 23:59:59',strtotime("+1 year"));


			$list_query = $list_query->where(function($q) use($query){
				$q = $q->where(function($q1) use($query){
					$q1 = $q1->whereNull('b.pay_time')->where('b.input_time', '>=', $query['start_at'])->where('b.input_time', '<=', $query['end_at']);
				})->orWhere(function($q2) use($query)	{
					$q2 = $q2->whereNotNull('b.pay_time')->where('b.input_time', '>=', $query['start_at'])->where('b.input_time', '<=', $query['end_at']);
				});
			});

			//chunmeng.jiang@live.com end


		$list_query = $list_query->select(DB::raw('a.collateral_licence_no, a.projects_name, b.pay_time, a.id, b.input_time, c.name, sum(b.money) as money, sum(b.profit) as profit, d.name, b.status'))->groupBy('b.project_id', 'b.input_time');

			//chunmeng.jiang@live.com start
			// 统计 还款笔数 金额合计 利息合计 本金合计
			$data2 = $list_query->get();

			$tj = [
				'bs' => 0,
				'money_sum' => 0,
				'profit_sum' => 0
			];

			foreach($data2 as $d){
				$tj['bs'] += 1;
				$tj['money_sum'] += $d->money;
				$tj['profit_sum'] += $d->profit;
			}

			//chunmeng.jiang@live.com end

		$list_query = $list_query->groupBy('b.project_id', 'b.input_time')
			->orderBy('b.status','asc')
			->orderBy('b.input_time','desc')
			->orderBy('a.id','desc');

		$data = $list_query->paginate(10);

		$data->appends($query);

		$links = $data->render();

		return view('admin.business.repayment_manager.index', compact('data', 'links', 'query', 'tj'));
	}

	/**
	 * 查询还款详情
	 * 
	 * @return [type] [description]
	 */
	public function repaymentInfo(string $time, int $project_id,Request $q){
		$re=$q->all();
		$query = DB::table('project_profits as a')
				->where('a.project_id', $project_id)
				->where('a.input_time', $time)
				->where('a.type', 1)
				->select('a.sn', 'a.user_name', 'a.money', 'a.profit', 'a.input_time', 'a.pay_time', 'a.status');

		$data = $query->paginate(15);

		$data->appends($re);
		
		$links = $data->render();

		return view('admin.business.repayment_manager.info', compact('data', 'links', 'name'));
	}
}